package com.hzzy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzzy.IDao.HzStudentMapper;
import com.hzzy.bean.HzClassCheckins;
import com.hzzy.bean.HzClassCourse;
import com.hzzy.bean.HzStudent;
import com.hzzy.enums.CheckinsStateEnum;
import com.hzzy.service.HzClassCheckinsService;
import com.hzzy.service.HzStudentService;

/**
 * 学生信息服务接口实现
 * @author 何剑波
 * 2016年5月11日
 */
@Service
public class HzStudentServiceImpl implements HzStudentService {
	
	@Autowired
	private HzStudentMapper hzStudentMapper;
	
	@Resource
	private HzClassCheckinsService hzClassCheckinsService;
	
	/**
	 * 根据学生学号查询该学生信息
	 * @param Sno 学生学号
	 * @return
	 */
	public HzStudent findBySno(String sno) {
		
		Map<String, Object[]> conditions = new HashMap<String, Object[]>();
		conditions.put("sno", new String[]{sno});
		List<HzStudent> list = hzStudentMapper.selectRelationByConditions(conditions);
		HzStudent hzStudent = null;
		if (list != null && list.size() > 0) {
			hzStudent = list.get(0);
		}
		return hzStudent;
	}

	/**
	 * 根据学生密码和学生学号找找信息
	 * @param sno
	 * @param password
	 * @return
	 */
	public HzStudent findBySnoAndPassword(String sno, String password) {
		
		Map<String, Object[]> conditions = new HashMap<String, Object[]>();
		conditions.put("sno", new String[] {sno});
		conditions.put("password", new String[] {password});
		conditions.put("student_state", new Integer[] {1} );	//是否正常就读
		List<HzStudent> list = hzStudentMapper.selectRelationByConditions(conditions);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	
	/**
	 * 根据班级Id查询所有学生的信息
	 */
	public List<HzStudent> findByClassId(int classId) {
		Map<String, Object[]> conditions = new HashMap<String, Object[]>();
		conditions.put("classId", new Integer[] {classId});
		return hzStudentMapper.selectPartialInfo(conditions);
	}

	/**
	 * 给点到过的学生附上状态
	 * @param studentList
	 * @param course
	 */
	public void addCheckinsState(List<HzStudent> studentList, HzClassCourse course) {
		if (course.getHzClassCheckins() != null) {	// 如果为空，说明是为点到的班级
			HzClassCheckins checkins = course.getHzClassCheckins();
			// 获取异常的学生
			List<HzClassCheckins> checkinsList = hzClassCheckinsService.getProblemStudentId(checkins.getSection(), checkins.getCourseid(),new Date());
			if (checkinsList != null && checkinsList.size() > 0 && studentList != null && studentList.size() > 0) {
				for (HzClassCheckins _checkins : checkinsList) {
					for (HzStudent student : studentList) {
						if (_checkins.getStudentId().equals(student.getSno())) {
							student.setCheckinsState(_checkins.getCcstate());
							break;
						}
					}
				}
			}
		}
	}

	/**
	 * 根据班级id，点到日期，学生名称，搜索数据
	 * @param classId
	 * @param ccdate
	 * @param studentName
	 * @return	返回Map  key:点到的状态 CheckinsStateEnum.java
	 */
	public int getAbnormalStudents(Integer classId, Date ccdate, String studentName,  Map<Integer, List<HzStudent>> studentStateMap) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("ccdate", ccdate);
		conditions.put("classId", classId);
		conditions.put("studentName", studentName);
		conditions.put("isFinish", 0);	//班级毕业状态
		List<HzStudent> list = hzStudentMapper.selectGroupByPartialInfo(conditions);
		if (list != null && list.size() > 0) {
			List<HzStudent> list_1 = new ArrayList<HzStudent>();
			List<HzStudent> list_2 = new ArrayList<HzStudent>();
			List<HzStudent> list_3 = new ArrayList<HzStudent>();
			for (HzStudent s : list) {
				if (s.getCheckinsState() == CheckinsStateEnum.ABSENTEEISM.getValue()) {
					list_2.add(s);
				} else if (s.getCheckinsState() == CheckinsStateEnum.LATE.getValue()) {
					list_1.add(s);
				} else if (s.getCheckinsState() == CheckinsStateEnum.LEAVE.getValue()) {
					list_3.add(s);
				}
			}
			studentStateMap.put(CheckinsStateEnum.ABSENTEEISM.getValue(), list_2.size() > 0?list_2 : null);
			studentStateMap.put(CheckinsStateEnum.LATE.getValue(), list_1.size() > 0?list_1 : null);
			studentStateMap.put(CheckinsStateEnum.LEAVE.getValue(), list_3.size() > 0?list_3 : null);
			return list.size();
		} else {
			return 0;
		}
	}

	/**
	 * 根据班级id，班级学期，学生名称，搜索数据
	 * @param classId
	 * @param semesterId	班级学期
	 * @param studentName
	 * @param studentStateMap
	 * @return
	 */
	public int getAbnormalStudents(Integer classId, Integer semesterId, String studentName,
			Map<Integer, List<HzStudent>> studentStateMap) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("semesterId", semesterId);
		conditions.put("classId", classId);
		conditions.put("studentName", studentName);
		conditions.put("isFinish", 0);	//班级毕业状态
		List<HzStudent> list = hzStudentMapper.selectGroupByPartialInfo(conditions);
		if (list != null && list.size() > 0) {
			List<HzStudent> list_1 = new ArrayList<HzStudent>();
			List<HzStudent> list_2 = new ArrayList<HzStudent>();
			List<HzStudent> list_3 = new ArrayList<HzStudent>();
			for (HzStudent s : list) {
				if (s.getCheckinsState() == CheckinsStateEnum.ABSENTEEISM.getValue()) {
					list_2.add(s);
				} else if (s.getCheckinsState() == CheckinsStateEnum.LATE.getValue()) {
					list_1.add(s);
				} else if (s.getCheckinsState() == CheckinsStateEnum.LEAVE.getValue()) {
					list_3.add(s);
				}
			}
			studentStateMap.put(CheckinsStateEnum.ABSENTEEISM.getValue(), list_2.size() > 0?list_2 : null);
			studentStateMap.put(CheckinsStateEnum.LATE.getValue(), list_1.size() > 0?list_1 : null);
			studentStateMap.put(CheckinsStateEnum.LEAVE.getValue(), list_3.size() > 0?list_3 : null);
			return list.size();
		} else {
			return 0;
		}
	}
	
	/**
	 * 根据学生Id和选定的时间，获取该学生点到异常记录
	 * @param studentId
	 * @param ccdate
	 * @return
	 */
	public List<HzStudent> findByStudentIdAndDate(String studentId, Date ccdate, int ccstate, int classId) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(studentId)) {
			conditions.put("studentId", studentId);
		}
		conditions.put("ccdate", ccdate);
		conditions.put("ccstate", ccstate);
		conditions.put("classId", classId);
		
		return hzStudentMapper.selectCheckinsStudentInfo(conditions);
	}

	/**
	 * 根据学生Id和选定的时间，获取该学生点到异常记录
	 * @param studnetId
	 * @param semesterId	班级学期Id
	 * @param ccstate 点到的状态
	 * @return
	 */
	public List<HzStudent> findByStudentIdAndSemesterId(String studentId, Integer semesterId, int ccstate,
			int classId) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(studentId)) {
			conditions.put("studentId", studentId);
		}
		conditions.put("semesterId", semesterId);
		conditions.put("ccstate", ccstate);
		conditions.put("classId", classId);
		
		return hzStudentMapper.selectCheckinsStudentInfo(conditions);
	}
}
